#!/bin/bash

if ! [ -n "$1" ];
then
  echo "init-evn-create.conf file not found"
  exit 1
fi

downloadFile(){
  wget -O $1 $2   > ${APP_DIR}/download.log 2>&1
  if ! [ -n "$(cat ${APP_DIR}/download.log | grep "200 OK")" ];
  then
    echo "$2 download failed"
    return 1
  fi
  return 0
}

exportParams(){
  PARAM_FILE=$1
  ! [ -a ${PARAM_FILE} ] && return 1
  PARAMS=($(cat ${PARAM_FILE} | grep -v '#' | awk NF))
  for PARAM in "${PARAMS[@]}"
  do
    KEY=$(echo "${PARAM}" | awk -F '=' '{print $1}')
    VALUE=$(echo "${PARAM}" | awk -F '=' '{print $2}')
    eval "${KEY}=${VALUE}"
  done
}
exportParams $1

#参数检测
if ! [ -n "${WGET_ADDRESS}" ];
then
  echo "wget address can not empty!!"
  exit 1
fi
! [ -n "${DEFAULT_INSTALL}" ] && DEFAULT_INSTALL="NO"
! [ -n "${DIR}" ] && DIR=$(pwd)
#变量初始化
INIT_ENV_DIR=${DIR}/init-env
INIT_ENV_PROP_FILE=${INIT_ENV_DIR}/global.properties
OLD_IFS="$IFS";IFS=",";ROOT_URLS=($WGET_ADDRESS);IFS="$OLD_IFS"
PAPPLICATIONS=${APPLICATIONS}
#分割应用
OLD_IFS="$IFS";IFS=",";APPLICATIONS=(${APPLICATIONS});IFS="$OLD_IFS"

#检测时候是否已安装wget、unzip
! [ -n "$(rpm -qa | grep wget)" ] && yum -y install wget > /dev/null 2>&1
! [ -n "$(rpm -qa | grep unzip)" ] && yum -y install unzip > /dev/null 2>&1

#创建init-env目录结构
mkdir -p ${INIT_ENV_DIR}
mkdir -p ${INIT_ENV_DIR}/apps

#拉取install.sh 输出全局配置到${INIT_ENV_PROP_FILE}
downloadFile ${INIT_ENV_DIR}/install.sh ${ROOT_URLS[0]}/install.sh;[ $? -eq 1 ] && exit 1
chmod 777 ${INIT_ENV_DIR}/install.sh
echo -e "APPLICATIONS=${PAPPLICATIONS}\n" > ${INIT_ENV_PROP_FILE}
echo -e "ROOT_DIR=${INIT_ENV_DIR}\n" >> ${INIT_ENV_PROP_FILE}
echo -e 'OUTPUT_DIR=${ROOT_DIR}/output\n' >> ${INIT_ENV_PROP_FILE}
echo -e "PRINT_LOG_ON_CONSOLE=YES\n" >> ${INIT_ENV_PROP_FILE}
echo -e "UNINSTALL_IF_INSTALLED=YES\n" >> ${INIT_ENV_PROP_FILE}


for APP in "${APPLICATIONS[@]}"
do
  echo "---------pull ${APP} package---------"
  OLD_IFS="$IFS";IFS=":";APP_INFOS=(${APP});IFS="$OLD_IFS"
  APP_NAME=$(echo "${APP}" | awk -F ':' '{print $1}')
  APP_VERSION=$(echo "${APP}" | awk -F ':' '{print $2}')
  APP_DIR=${INIT_ENV_DIR}/apps/${APP_NAME}
  mkdir -p ${APP_DIR}
  SUCCESS_FLAG=NO
  for ROOT_URL in "${ROOT_URLS[@]}"
  do
    echo "download ${APP_NAME} from ${ROOT_URL}"
    #拉取默认版本号
    if ! [ -n "${APP_VERSION}" ];
    then
      echo "pull ${APP_NAME} default version"
      #拉取默认版本
      downloadFile ${APP_DIR}/default_version ${ROOT_URL}/apps/${APP_NAME}/default_version;[ $? -eq 1 ] && continue
      APP_VERSION=$(cat ${APP_DIR}/default_version)
    fi
    rm -rf ${APP_DIR:?}/${APP_VERSION:?}
    ZIP_FILE_NAME=${APP_NAME}_${APP_VERSION}.zip
    #拉取
    downloadFile ${APP_DIR}/${ZIP_FILE_NAME} ${ROOT_URL}/apps/${APP_NAME}/zip/${ZIP_FILE_NAME};[ $? -eq 1 ] && continue
    #解压
    unzip -o ${APP_DIR}/${ZIP_FILE_NAME} -d ${APP_DIR} > /dev/null 2>&1
    chmod 777 ${APP_DIR}/${APP_VERSION}/${APP_NAME}_${APP_VERSION}_install.sh
    #检测目录下是否含哟ini文件 如果有将配置信息刷入init-env.properties
    if [ -a ${APP_DIR}/${APP_VERSION}/${APP_NAME}_${APP_VERSION}_config.ini ];
    then
      echo -e "\n#[${APP_NAME}]" >> ${INIT_ENV_PROP_FILE}
      cat ${APP_DIR}/${APP_VERSION}/${APP_NAME}_${APP_VERSION}_config.ini >> ${INIT_ENV_PROP_FILE}
      rm -rf ${APP_DIR}/${APP_VERSION}/${APP_NAME}_${APP_VERSION}_config.ini
    fi
    #删除log 和 zip
    rm -rf ${APP_DIR}/download.log
    rm -rf ${APP_DIR:?}/${ZIP_FILE_NAME:?}
    SUCCESS_FLAG=YES
    break
  done
  [ ${SUCCESS_FLAG} == NO ] && echo "${APP_NAME} download failed"
done

#是否默认安装
[ ${DEFAULT_INSTALL} == YES ] && ${INIT_ENV_DIR}/install.sh ${INIT_ENV_PROP_FILE}

